import QtQuick 2.15
import QtQuick.Layouts 1.15
import QtQuick.Controls 2.15

import MyApp 1.0
import Device 1.0

Item {
    id: dev_manage
    Device {
        id: empty_device
    }
    property var cur_dev: empty_device
    property var result_chart

    Connections {
        target: device_list
        function onCur_indexChanged() {
            dev_manage.cur_dev = server.deviceList[device_list.cur_index] ? server.deviceList[device_list.cur_index] : empty_device
        }
    }

    ColumnLayout {
        anchors.fill: parent
        RowLayout {
            spacing: 10
            Layout.bottomMargin: 10
            Layout.fillHeight: true
            Label {
                Layout.alignment: Qt.AlignVCenter
                text: "设备管理"
                font.pixelSize: 60
            }
            Rectangle {
                Layout.alignment: Qt.AlignBottom
                Layout.leftMargin: 20
                Layout.bottomMargin: 12
                width: 25
                height: 25
                radius: 15
                color: {
                    switch (dev_manage.cur_dev.device_status) {
                    case Types.IDLE:
                        return "green"
                    case Types.MEASURING:
                        return "yellow"
                    case Types.UNKNOWN:
                        return "yellow"
                    case Types.TIMEOUT:
                        return "red"
                    case Types.DISCONNECTED:
                        return "red"
                    default:
                        return "red"
                    }
                }
            }
            Label {
                id: status_label
                Layout.alignment: Qt.AlignBottom
                property string currentText
                currentText: {
                    switch (dev_manage.cur_dev.device_status) {
                    case Types.IDLE:
                        return "IDLE"
                    case Types.MEASURING:
                        return "MEASURE"
                    case Types.UNKNOWN:
                        return "WAITING"
                    case Types.TIMEOUT:
                        return "TIMEOUT"
                    case Types.DISCONNECTED:
                        return "OFFLINE"
                    default:
                        return "UNDEFINED"
                    }
                }

                font.pixelSize: 40
                opacity: 1.0

                SequentialAnimation {
                    id: status_fade
                    running: false
                    NumberAnimation {
                        target: status_label
                        property: "opacity"
                        to: 0
                        duration: 100 // 淡出动画
                    }
                    ScriptAction {
                        script: {
                            // 在淡出结束时更新文本
                            status_label.text = status_label.currentText // 保存当前的文本内容
                        }
                    }
                    NumberAnimation {
                        target: status_label
                        property: "opacity"
                        to: 1
                        duration: 100 // 淡入动画
                    }
                }

                // 每次文字变化时触发淡出和淡入
                onCurrentTextChanged: {
                    status_fade.start()
                }
            }
        }
        Label {
            text: "设备ID: " + dev_manage.cur_dev.device_id
            font.pixelSize: 35
        }
        Label {
            text: "最近更新: " + dev_manage.cur_dev.last_update
            font.pixelSize: 35
        }

        RowLayout {
            Layout.topMargin: 10
            Layout.fillHeight: true
            Layout.fillWidth: true // Ensure the RowLayout fills width as well

            spacing: 20
            Rectangle {
                Layout.preferredHeight: 650
                Layout.preferredWidth: 700
                height: parent.height
                color: "aliceblue"
                radius: 10
                ColumnLayout {
                    anchors.fill: parent
                    anchors.margins: 20
                    Label {
                        text: "测量历史"
                        Layout.bottomMargin: 10
                        font.pixelSize: 40
                    }
                    ListView {
                        id: result_list
                        clip: true
                        Layout.fillHeight: true
                        Layout.fillWidth: true // Ensure ListView also fills the available space
                        ScrollBar.vertical: ScrollBar {
                            policy: ScrollBar.AlwaysOn
                        }
                        spacing: 15
                        model: dev_manage.cur_dev.results
                        delegate: Rectangle {
                            anchors {
                                left: parent.left
                                right: parent.right
                                rightMargin: 20
                            }

                            height: 120
                            radius: 20
                            color: "white" // Ensure delegate has a background to see the content

                            ColumnLayout {
                                anchors {
                                    fill: parent
                                    margins: 15
                                }
                                RowLayout {
                                    Layout.fillWidth: true
                                    Layout.fillHeight: true
                                    Label {
                                        text: "启动："
                                        font.pixelSize: 30
                                    }
                                    Item {
                                        Layout.fillWidth: true
                                    }
                                    Label {

                                        text: modelData.create_time
                                        font.pixelSize: 30
                                    }
                                }

                                RowLayout {
                                    Layout.fillWidth: true
                                    Layout.fillHeight: true
                                    Label {
                                        text: "激励电流(Amp)："
                                        font.pixelSize: 30
                                    }
                                    Label {
                                        text: switch (modelData.driving_current) {
                                              case Types.AMPL_A15:
                                                  return "0.15A"
                                              case Types.AMPL_A25:

                                                  return "0.25A"
                                              case Types.AMPL_A5:

                                                  return "0.5A"
                                              case Types.AMPL_A8:
                                                  return "0.8A"
                                              case Types.AMPL_1A:
                                                  return "1A"
                                              }
                                        font.pixelSize: 30
                                    }
                                    Item {
                                        Layout.fillWidth: true
                                    }
                                    Label {
                                        text: "频点数："
                                        font.pixelSize: 30
                                    }
                                    Label {
                                        text: modelData.points.length
                                        font.pixelSize: 30
                                    }
                                    // Item {
                                    //     Layout.fillWidth: true
                                    // }
                                }
                            }
                            MouseArea {
                                anchors.fill: parent
                                onClicked: {
                                    result_chart.show(modelData)
                                }
                            }
                        }
                    }
                }
            }

            ColumnLayout {
                Layout.fillWidth: true
                Layout.fillHeight: true
                Layout.margins: 20
                spacing: 10

                Label {
                    text: "测量状态"
                    font.pixelSize: 40
                }

                RowLayout {
                    Label {
                        text: "当前："
                        font.pixelSize: 30
                    }

                    Label {
                        id: measure_label
                        property string currentText
                        currentText: cur_dev.device_status === Types.MEASURING ? "测量中" : "停止"
                        font.pixelSize: 30
                        opacity: 1.0

                        SequentialAnimation {
                            id: measure_fade
                            running: false
                            NumberAnimation {
                                target: measure_label
                                property: "opacity"
                                to: 0
                                duration: 100 // 淡出动画
                            }
                            ScriptAction {
                                script: {
                                    // 在淡出结束时更新文本
                                    measure_label.text = measure_label.currentText // 保存当前的文本内容
                                }
                            }
                            NumberAnimation {
                                target: measure_label
                                property: "opacity"
                                to: 1
                                duration: 100 // 淡入动画
                            }
                        }

                        // 每次文字变化时触发淡出和淡入
                        onCurrentTextChanged: {
                            measure_fade.start()
                        }
                    }
                }

                Label {
                    text: "电池电压：" + roundToDecimals(cur_dev.cur_voltage, 2)
                    font.pixelSize: 30
                }

                Label {
                    text: "当前频率：" + cur_dev.cur_freq
                    font.pixelSize: 30
                }

                ProgressBar {
                    Layout.fillWidth: true
                    value: (cur_dev.cur_freq_index + 1) / cur_dev.freq_num
                }

                Label {
                    Layout.topMargin: 10
                    text: "定时任务"
                    font.pixelSize: 40
                }

                Label {
                    text: "数量：" + cur_dev.tasks.length.toString()
                    font.pixelSize: 30
                }

                Item {
                    Layout.fillHeight: true
                }

                Label {
                    // Layout.topMargin: 100
                    Layout.topMargin: 10
                    text: "EIS测量"
                    font.pixelSize: 40
                }

                RowLayout {
                    spacing: 10
                    Label {
                        text: "激励电流:"
                        font.pixelSize: 30
                    }

                    Item {
                        Layout.fillWidth: true
                    }

                    ComboBox {
                        id: comboBox
                        Layout.preferredWidth: 300
                        Layout.preferredHeight: 60

                        font.pixelSize: 30

                        // width: parent.width
                        property int selectedValue: Types.AMPL_1A
                        currentIndex: 4

                        model: ["0.15A", "0.25A", "0.5A", "0.8A", "1A"]

                        // 选中时更新枚举值
                        onCurrentIndexChanged: {
                            switch (comboBox.currentIndex) {
                            case 0:
                                selectedValue = Types.AMPL_A15
                                break
                            case 1:
                                selectedValue = Types.AMPL_A25
                                break
                            case 2:
                                selectedValue = Types.AMPL_A5
                                break
                            case 3:
                                selectedValue = Types.AMPL_A8
                                break
                            case 4:
                                selectedValue = Types.AMPL_1A
                                break
                            }
                        }
                        popup.font.pixelSize: 30
                    }
                }

                Button {
                    Layout.topMargin: 20
                    Layout.fillWidth: true
                    Layout.preferredHeight: 100
                    text: "启动测量"
                    enabled: cur_dev.device_status === Types.IDLE
                    onClicked: cur_dev.newEIS(comboBox.selectedValue)
                    font.pixelSize: 40
                }
            }
        }
        Item {
            Layout.fillHeight: true
        }
    }

    function roundToDecimals(num, decimals) {
        var factor = Math.pow(10, decimals)
        return Math.round(num * factor) / factor
    }
}
